using System;
using System.Text;
using System.Windows.Forms;

namespace ctsu.Admin.Forms.CoreLab
{
	public partial class TestReferenceEditDlg : Form
	{
		public delegate void ButtonClickEventHandler(object sender, EditBatchEventArgs e);
		public event ButtonClickEventHandler ButtonClick;

		public TestReferenceEditDlg()
		{
			InitializeComponent();
		}

		private void btnReplaceSelected_Click(object sender, System.EventArgs e)
		{
			EditBatchEventArgs eva;
			eva = SetEventArgs();

			if (!VerifyReplaceColumns(eva))
				return;

			eva.ButtonName = "ReplaceSelected";
			if (ButtonClick != null)
				ButtonClick(this, eva);
		}

		private void btnReplaceAll_Click(object sender, System.EventArgs e)
		{

			EditBatchEventArgs eva;
			eva = SetEventArgs();

			if (!VerifyReplaceColumns(eva))
				return;

			StringBuilder sb = new StringBuilder("You are about to replace 'ALL' rows for selected columns with changes.");
			sb.Append(Environment.NewLine);
			sb.Append("Press 'Yes' to continue.");
			if (MessageBox.Show(this, sb.ToString(), "Confirm To Continue", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
				return;

			eva.ButtonName = "ReplaceAll";
			if (ButtonClick != null)
				ButtonClick(this, eva);
		}

		private bool VerifyReplaceColumns(EditBatchEventArgs eva)
		{
			foreach (bool b in eva.ReplaceColumns)
			{
				if (b)
				{
					return true;
				}
			}
			MessageBox.Show(this, "Select a replacement column", "Select Replacement Column", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return false;
		}

		private EditBatchEventArgs SetEventArgs()
		{
			EditBatchEventArgs e = new EditBatchEventArgs();

			e.ReplaceColumns[0] = chkAll.Checked;
			e.ReplaceColumns[1] = chk1.Checked;
			e.ReplaceColumns[2] = chk2.Checked;
			e.ReplaceColumns[3] = chk3.Checked;

			return e;
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}